﻿using Core.Log;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;
using System.Windows.Input;

namespace ModBus.ViewModels
{
    public class SubViewModel : BindableBase , IDialogAware
    {
        private string message;
        public string Message
        {
            get { return message; }
            set { SetProperty(ref message, value); }
        }

        private DialogCloseListener requestClose;
        public DialogCloseListener RequestClose
        {
            get { return requestClose; }
            set { requestClose = value; }
        }
        public ICommand ClickYesCmd
        { get => new DelegateCommand(ClickYes); }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {          
        }

        private void ClickYes()
        {
            try
            {
                //点击Yes按钮的时候关闭对话框,并且传递对话框结果（结果中还可以带参数）
                IDialogParameters parameters = new DialogParameters();
                parameters.Add("param1", Message);
                RequestClose.Invoke(parameters, ButtonResult.OK);
            }
            catch (System.Exception ex)
            {
                NotifyCollect.NotifyContainer["Error"].Add(ex.ToString());
            }         
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            //窗体打开时获取传递过来的参数
           //Message = parameters.GetValue<string>("param1");
        }
    }
}
